package com.mdms.jsf.managedbeans.dictionaries;

import java.util.ArrayList;
import java.util.List;

import com.mdms.commands.dictionaries.DictionariesCommand;
import com.mdms.dto.MdmDctAttributes;
import com.mdms.dto.MdmDictionaries;
import com.mdms.jsf.managedbeans.BaseMBean;
import com.mdms.jsf.managedbeans.menu.NavigationMenuMBean;
import com.mdms.logger.SimplyMDLogger;
import com.mdms.utils.FacesUtils;

public class DictionariesMBean extends BaseMBean {

	private final static SimplyMDLogger logger = new SimplyMDLogger(DictionariesMBean.class);
	private static final long serialVersionUID = 1L;
	
	private String aDct_type;
	
	private String aDct_code;
	
	private String aDct_name;
	
	private List dictTreeList = new ArrayList();
	
	
	public String addNewDictionary(){
		String methodName="addNewDictionary";
		MdmDictionaries dictToSave = new MdmDictionaries();
		MdmDctAttributes attrToSave = new MdmDctAttributes();
		dictToSave.setDct_type(getaDct_type());
		dictToSave.setDct_code(getaDct_code());
		try{
		DictionariesCommand command = new DictionariesCommand();
		command.setAction(DictionariesCommand.SAVE_DICT);
		command.setMdmDictionaries(dictToSave);
		command.execute();
		if(command.errorCode==0){
			dictToSave=command.getMdmDictionaries();
			attrToSave.setDat_name("DCT_NAME");
			attrToSave.setDat_svalue(aDct_type);
			attrToSave.setMdmDictionaries(dictToSave);
			command = new DictionariesCommand();
			command.setAction(DictionariesCommand.SAVE_DICT_ATT);
			command.setMdmAttributes(attrToSave);
			command.execute();
			if(command.getErrorCode()==0){
				dictToSave.getMdmDctAttributeses().add(command.getMdmAttributes());
				getDictTreeList().add(dictToSave);
				FacesUtils.showInform("Poprawnie zapisano slownik");
			}
		}else{
			logger.error(methodName, command.getErrorCode(), command.getErrorDesc());
			FacesUtils.showError("Nie udalo sie :(");
		}
		}catch (Exception e){
			logger.error(methodName, e);
			FacesUtils.showError(e.getMessage());
		}
		return "";
	}

	public String getaDct_type() {
		return aDct_type;
	}

	public void setaDct_type(String aDct_type) {
		this.aDct_type = aDct_type;
	}

	public String getaDct_code() {
		return aDct_code;
	}

	public void setaDct_code(String aDct_code) {
		this.aDct_code = aDct_code;
	}

	public String getaDct_name() {
		return aDct_name;
	}

	public void setaDct_name(String aDct_name) {
		this.aDct_name = aDct_name;
	}

	public List getDictTreeList() {
		/*if(dictTreeList==null){
			dictTreeList=DictionariesCache.getDictionaries();
		}*/
		return dictTreeList;
	}

	public void setDictTreeList(List dictTreeList) {
		this.dictTreeList = dictTreeList;
	}
	
	

}
